<!doctype html>
<html lang="zh-CN">
<head><title>测试数据</title>


</head>

{//<script src="https://cdn.static.runoob.com/libs/jquery/1.8.0/jquery.min.js"></script>}
<link rel="stylesheet" href="https://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="https://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">

<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" src="__PUBLIC__/static/js/scripts/jquery-2.1.1.js"></script>

<script type="text/javascript">

//    var availableTags = [
//        {volist name="stockname" id="vo"}
//        "编号：{$vo.number},品名：{$vo.name}，规格：{$vo.spec}",
////        {/volist}
////    ];
//        $(function() {
//            $("#tags").autocomplete({
//                source: availableTags
//                onSelect: function(suggestion) {
//                    $('#show').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
//                },
//            });
//
//        })


</script>
<style type="text/css">
    input {width:100%}
    div , th {text-align: center;}
</style>

<body class="container" style="font-size:10px">
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-plus"></i>增加</button>

<button type="button" class="btn btn-success" ><i class="glyphicon glyphicon-ok"></i>保存</button>

<div class="row ">
    <form id="myForm" role="form" >
        <table  class="table table-hover table-bordered" >
            <tr  class="info ">
                <th >编号</th>
                <th >仓库</th>
                <th >名称</th>
                <th >厂家</th>
                <th >规格</th>
                <th >单位</th>
                <th >单价</th>
                <th >库存</th>
                <th >数量</th>
                <th >功能</th>
            </tr>
        </table>
    </form>
</div>



<!--<input type="text" id="tags" name="name[]" value="" /><span class="glyphicon glyphicon-search"></span>搜索-->
    <!--&lt;!&ndash;<input id="tags">&ndash;&gt;-->

<!--<div id="show">-->


<script type="text/javascript">
//   var id = 1;
    var number = {$max};
    $(".btn-primary").click(function(){
        if($("table tr").hasClass("addtr")){
            alert("先完成操作！！！");
        }else{
            $("table tr:last").after(' <tr class="addtr ">'+
                '<td><input type="text" readonly="readonly" name="number[]" value="'+number+'" /></td>'+
                '<td><select name="category_id[]" >{foreach name="category" id="vo"}' +
                '<option value="{$vo.id}" {switch $vo.id}{case 1}"selected"{/case}' +
                '{/switch}>{$vo.name} </option>{/foreach}</select></td>' +
                '<td><input list="pasta" id="show"><datalist id="pasta">' +
                '{volist name="stockname" id="vo"}<option>"编号：{$vo.number},品名：{$vo.name}，规格：{$vo.spec}"</option>{/volist}</datalist><a href="javascript:;" class="btn serach btn-default btn-xs"><span class="glyphicon glyphicon-search"></span>确定</a></td>' +
                '<td><input type="text" name="dimension[]" value="" /></td>' +
                '<td><input type="text" name="spec[]" value="" /></td>' +
                '<td><input type="text" name="unit[]" value="" /></td>' +
                '<td><input type="text" name="price[]" value="" /></td>' +
                '<td><input type="text" disabled="disabled" name="quantitys[]" value="" /></td>' +
                '<td><input type="text" name="quantity[]" value="" /></td>' +
                '<td> <a href="javascript:;" class="btn save btn-default btn-xs">保存</a>  <a href="javascript:;" class="btn off btn-default btn-xs">取消</a> </td>'+
                ' </tr>');
            number ++;
        }
    });

//    $(function() {
//
//
//    });

    //保存
    $(document).on("click",".serach",function(){
        var that = $(this);
        var REname =that.parent().parent().find('td').eq(2).find('input').val().match(/：(\S*),/);
        var sname = REname[1];
//        alert(that.parent().parent().find('td').eq(2).find('input').val())
        var scategorytext =that.parent().parent().find('td').eq(1).find("option:selected").text();
        var squantity =that.parent().parent().find('td').eq(8).find('input').val();

            $.ajax({
                url: "{:url('index/stockpile/index')}",
                data:"number="+sname+"&time="+Math.random(),
                type: "get",
                dataType:"json",
                success:function(data){
                    if(data.code !=1){
                        alert(data.msg);
                    }else {
                        var v = data.data;
                        var snumber = v.number;
                        var scategory = v.category_id;
                        var sname = v.name;
                        var sdimension = v.dimension;
                        var sspec = v.spec;
                        var sunit = v.unit;
                        var sprice = v.price;
                        var squantitys = v.quantity;
                        var n="";
                        n+='<td><input type="text" readonly="readonly" name="number[]" value="'+ snumber +'" /></td>';
                        n+='<td><select disabled="disabled" name="category_id[]" ><option value="'+ scategory +'" >'+scategorytext+'</option></select></td>';
                        n+='<td><input type="text" disabled="disabled" name="name[]" value="'+ sname +'" /></td>';
                        n+='<td><input type="text" disabled="disabled" name="dimension[]" value="'+ sdimension +'" /></td>';
                        n+='<td><input type="text" disabled="disabled"" name="spec[]" value="'+ sspec +'" /></td>';
                        n+='<td><input type="text" disabled="disabled" name="unit[]" value="'+ sunit +'" /></td>';
                        n+='<td><input type="text" disabled="disabled" name="price[]" value="'+ sprice +'" /></td>';
                        n+='<td><input type="text" disabled="disabled" name="quantitys[]" value="'+ squantitys +'" /></td>';
                        n+='<td><input type="text"  name="quantity[]" value="'+ squantity +'" /></td>';
                        n+='<td>';
//                            n+='<a href="javascript:void(0);" class="btn edit btn-default btn-xs"><span class="icon-edit"></span>修改</a>';
                        n+='<a href="javascript:;" class="btn save btn-default btn-xs">保存</a>  <a href="javascript:;" class="btn off btn-default btn-xs">取消</a> ';
                        n+='</td>';
//                            that.parent().parent().removeClass("addtr");
                        that.parent().parent().html(n);

                    }
                }
            });
            return false;



    });

    $(document).on("click",".save",function(){
        var par = $(this).parent().parent().find('td');
        var snumber =par.eq(0).find('input').val();
        var scategory =par.eq(1).find('select').val();
        var scategorytext =par.eq(1).find("option:selected").text();
        var sname =par.eq(2).find('input').val();
        var sdimension =par.eq(3).find('input').val();
        var sspec =par.eq(4).find('input').val();
        var sunit =par.eq(5).find('input').val();
        var sprice =par.eq(6).find('input').val();
        var squantitys =par.eq(7).find('input').val();
        var squantity =par.eq(8).find('input').val();
        if(isNaN(squantity)) {
            alert("数量只能为数字");
        }else if(squantity.length == 0) {
            alert('入库数量不能为空');
        }else if(squantity == 0){
            alert('入库数量不能为零');
        }else{
            var n="";
            n+='<td><input type="text" readonly="readonly" name="number[]" value="'+ snumber +'" /></td>';
            n+='<td><select readonly="readonly" name="category_id[]" ><option value="'+ scategory +'" >'+ scategorytext +'</option></select></td>';
            n+='<td><input type="text" readonly="readonly" name="name[]" value="'+ sname +'" /></td>';
            n+='<td><input type="text" readonly="readonly" name="dimension[]" value="'+ sdimension +'" /></td>';
            n+='<td><input type="text" readonly="readonly" name="spec[]" value="'+ sspec +'" /></td>';
            n+='<td><input type="text" readonly="readonly" name="unit[]" value="'+ sunit +'" /></td>';
            n+='<td><input type="text" readonly="readonly" name="price[]" value="'+ sprice +'" /></td>';
            n+='<td><input type="text" disabled="disabled" name="quantitys[]" value="'+ squantitys +'" /></td>';
            n+='<td><input type="text" readonly="readonly" name="quantity[]" value="'+ squantity +'" /></td>';
            n+='<td>';
            n+='<a href="javascript:;" class="btn edit btn-default btn-xs"><span class="icon-edit"></span>修改</a>';
            n+='</td>';


            $(this).parent().parent().removeClass("addtr");
            $(this).parent().parent().html(n);
        }
        return false;

    });

    //修改
    $(document).on("click",".edit",function(){
        if($("table tr").hasClass("addtr")){
            alert("先完成操作！！！");
        }else{
            var par = $(this).parent().parent().find('td');
            var enumber =par.eq(0).find('input').val();
            var ecategory =par.eq(1).find('select').val();
            var ecategorytext =par.eq(1).find("option:selected").text();
            var ename =par.eq(2).find('input').val();
            var edimension =par.eq(3).find('input').val();
            var espec =par.eq(4).find('input').val();
            var eunit =par.eq(5).find('input').val();
            var eprice =par.eq(6).find('input').val();
            var equantitys =par.eq(7).find('input').val();
            var equantity =par.eq(8).find('input').val();

            var n="";
            n+='<td><input type="text"  readonly="readonly" name="number[]" value="'+enumber+'" /></td>';
            n+='<td><select name="category_id[]" >{foreach name="category" id="vo"}' +
                '<option value="{$vo.id}" {switch $vo.id}{case 1}"selected"{/case}' +
                '{/switch}>{$vo.name} </option>{/foreach}</select></td>';
            n+='<td><input type="text"  name="name[]" value="'+ename+'" /></td>';
            n+='<td><input type="text"  name="dimension[]" value="'+edimension+'" /></td>';
            n+='<td><input type="text"  name="spec[]" value="'+espec+'" /></td>';
            n+='<td><input type="text"  name="unit[]" value="'+eunit+'" /></td>';
            n+='<td><input type="text"  name="price[]" value="'+eprice+'" /></td>';
            n+='<td><input type="text" disabled="disabled" name="quantitys[]" value="'+equantitys+'" /></td>';
            n+='<td><input type="text" name="quantity[]" value="'+equantity+'" /></td>';
            n+='<td>';
            n+='<a href="javascript:;" class="btn save btn-default btn-xs">保存</a>';
            n+='<a href="javascript:;" class="btn off btn-default btn-xs">取消</a>';
            n+='</td>';
            $(this).parent().parent().addClass("addtr");
            $(this).parent().parent().html(n);
        }

    });

    $(document).on("click",".off",function(){
//        var con;
//        con=confirm("你确定删除此行吗?"); //在页面上弹出对话框
//        if(con==true){
//            $(this).parent().parent().removeClass("addtr");
//            $(this).parent().parent().empty("addtr");
//        }else {
//            return false;
//        }




        window.location.reload();
    });



    $(function(){
        var flag = false;
        $(".btn-success").click(function () {
            var data = $('#myForm').serialize();
            if(!data) {
                alert('数据不能为空');
            }else {
                if($("table tr").hasClass("addtr")){
                    alert("先完成操作！！！");
                }else {
                    if(!flag){
                        $.ajax({
                            url: "{:url('index/instock/addItem')}",
                            data: data,
                            type: "post",
                            dataType: "json",
                            success: function (data) {
                                if (data.code != 1) {
                                    alert(data.msg);
                                } else {
                                    alert(data.msg);
                                    window.location.href="{:url('index/instock/index')}";
                                }
                            }
                        });
                        //                       return flag =true;

                    }else {
                        alert('你确定需要重复提交？');
                    }
                }
            }
        });

    });


alert(Snumber)
</script>

<script src="__PUBLIC__/static/js/form.js"></script>
<script>
//    $(function() {
// //       var snumber =$(this).find('input').val();
////        var availableTags = [
////            {volist name="stockname" id="vo"}
////            "{$vo.name}",
////            {/volist}
////        ];
//
//
//
// //       $("#show").html(snumber)
//    });

</script>

</body>
</html>

